DOCKER_COMPOSE = docker compose -p {{ cookiecutter.package_name }}

.PHONY: help format lint up up-d down logs ps {% if cookiecutter.orm != "beanie" and cookiecutter.include_alembic == "yes" %}migrate-create migrate-up migrate-down{% endif %} seed test

help:
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-20s\033[0m %s\n", $$1, $$2}'

format: ## black + isort
	black --line-length 100 --exclude "alembic|venv" .
	isort --skip alembic --skip venv .

lint: ## flake8
	flake8 --exclude alembic,venv --max-line-length 100 app

up: ## Start containers (build + tail)
	$(DOCKER_COMPOSE) up --build

up-d: ## Start containers detached
	$(DOCKER_COMPOSE) up --build -d

down: ## Stop + remove containers
	$(DOCKER_COMPOSE) down

logs: ## Tail container logs
	$(DOCKER_COMPOSE) logs -f

ps: ## Show containers
	$(DOCKER_COMPOSE) ps

{% if cookiecutter.orm != "beanie" and cookiecutter.include_alembic == "yes" %}
migrate-create: ## New autogenerated migration (asks message)
	@read -p "Migration message: " msg; \
	$(DOCKER_COMPOSE) exec api alembic revision --autogenerate -m "$$msg"

migrate-up: ## Apply pending migrations
	$(DOCKER_COMPOSE) exec api alembic upgrade head

migrate-down: ## Rollback last
	$(DOCKER_COMPOSE) exec api alembic downgrade -1
{% endif %}

seed: ## Run init seeders
	$(DOCKER_COMPOSE) exec api python3 -m app.scripts.init

test: ## Run tests
	$(DOCKER_COMPOSE) exec api pytest
